package com.honey.mybatis.generator.dao.method;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.IfBlock;
import com.honey.compilation.java.Interface;
import com.honey.compilation.java.JavaKeyWord;
import com.honey.compilation.java.Method;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.types.JDKFullyQualifiedJavaType;
import com.honey.mybatis.generator.MybatisPlugin;
import com.honey.mybatis.generator.dao.DaoConstant;

/**
 * 按照primaryKey查询
 * @author Administrator
 *
 */
public class UpdateByDynamicMethod extends AbstractCreateInterfaceMethod{

	private static final String METHOD_NAME="updateByDynamic";
	
	private static final FullyQualifiedJavaType RETURN_TYPE = JDKFullyQualifiedJavaType.getIntInstance();
	
	private FullyQualifiedJavaType UPDATE_TYPE;
	
	private boolean isUseSession = false ;
	
	public UpdateByDynamicMethod(
			AbstractJavaCompilation clazz, MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn
			) {
		this(clazz, belongPlugin, introspectSchema, introspectSchemaColumn, false);
	}
	
	public UpdateByDynamicMethod(
			AbstractJavaCompilation clazz, MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn,
			boolean  isUseSession
			) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
		this.isUseSession = isUseSession;
		if( clazz instanceof Interface ){
			UPDATE_TYPE = new FullyQualifiedJavaType("E");
		}else{
			UPDATE_TYPE = super.getEntityClassType(introspectSchema.getSchema());
		}
	}
	
	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#addBody(com.honey.compilation.java.Method)
	 */
	@Override
	protected void addBody(Method method) {	
		IfBlock ifBlock = null;
		if( isUseSession()){
			ifBlock = new IfBlock(new StringBuilder().append( DaoConstant.SQL_SESSION_NAME ).append(" == null").toString());
			ifBlock.addBodyLine("return -1");
			method.addBlocks(ifBlock);
		}
		ifBlock = new IfBlock(new StringBuilder()
			.append(DaoConstant.CONDITION_VARIABLE_NAME)
			.append(" == null") 
			.append( " || "+ DaoConstant.CONDITION_VARIABLE_NAME +".getOredCriteria().size() == 0" )
			.toString()
		);
		ifBlock.addBodyLine(DaoConstant.CONDITION_VARIABLE_NAME+" = null");
		method.addBlocks(ifBlock);
		method.addBodyLine(" Map<String, Object> parameter = new HashMap<String, Object>() ");
		method.addBodyLine("parameter.put(\""+DaoConstant.MAPPER_RECORD+"\", "+super.getEntityVariableName(introspectSchema.getSchema())+") ;");
		method.addBodyLine("parameter.put(\"example\", "+DaoConstant.CONDITION_VARIABLE_NAME+") ;");
		method.addBodyLine(
				new StringBuilder()
				.append(JavaKeyWord.RETURN)
				//.append("super.")
				.append(mybatisPlugin.getDependentModel().call(null))
				.append("update (")
				.append( isUseSession()?DaoConstant.SQL_SESSION_NAME+" , ":"" )
				//.append(DaoConstant.UPDATE_BY_DYNAMIC_FIELD_NAME)
				.append(super.getMapperId().getUpdateByDynamicMapperId())
				.append(" , parameter )")
				.toString()
		);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodName()
	 */
	@Override
	protected String getMethodName() {		
		String answer = null;
		if( mybatisPlugin.isEnableUpdate() ){
			if( !isUseSession ){
				answer = METHOD_NAME;
			}else if( isUseSession  && mybatisPlugin.isEnableUseSqlSession() ){
				answer = METHOD_NAME;
			}
		}
		return answer;
	}
	
	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.dao.method.AbstractCreateInterfaceMethod#getUpdateEntityType()
	 */
	@Override
	public FullyQualifiedJavaType getUpdateEntityType() {
		
		return UPDATE_TYPE;
	}

	/**
	 * @return the isUseSession
	 */
	@Override
	public final boolean isUseSession() {
		return isUseSession;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getJavaDoc()
	 */
	@Override
	protected String[] getJavaDoc() {
		String[] answer = new String[]{
			"动态条件更新记录集,主键和Blob字段不被更新。方法返回 影响 记录集的数量。"
		};
		return answer;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getReturnType()
	 */
	@Override
	protected FullyQualifiedJavaType getReturnType() {
		
		return RETURN_TYPE;
	}
	
}
